home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / rand.zip / RAND.PAS < prev    next >
Pascal/Delphi Source File  |  1992-08-26  |  2KB  |  52 lines

  1. Unit rand;
  2.  
  3. {RAND.TPU and RAND.PAS - Created August 27, 1992
  4.  By Ray Sun
  5.  
  6.      RAND.PAS provides good random numbers for pascal users.  The
  7. numbers provided should show a statistically acceptable amount of runs
  8. and the average of the numbers should approach half of the input
  9. integer i.  I credit a book of whose name I unfortunately cannot
  10. remember for the algorithm used for the rectangularly distributed
  11. random numbers.  I merely implemented the algorithm as a pascal unit
  12. for the public to use.
  13.  
  14.      On the use of this code, please mention my name in the
  15. documentation.  Thank you.
  16. }
  17.  
  18. Interface
  19.  
  20. Var x, y, z : real;                                      { 3 seed values }
  21.  
  22. Function RandomNumber( i : integer ):integer;
  23. Procedure Initialize;
  24.  
  25. Implementation
  26.  
  27. Function RandomNumber( i : integer ):integer;
  28. Begin
  29.  
  30. {    x:=171 * (ix mod 177) - 2 * (ix / 177);              (* int random *)
  31.      y:=172 * (iy mod 177) - 35 * (iy / 176);
  32.      z:=170 * (iz mod 177) - 63 * (iz / 178);
  33.      if x<0 then x:=ix+30269;
  34.      if y<0 then y:=iy+30307;
  35.      if z<0 then z:=iz+30323;                }
  36.  
  37.      x := (trunc( 171 * x ) mod 30269)*1.0;               { longint random }
  38.      y := (trunc( 172 * x ) mod 30307)*1.0;
  39.      z := (trunc( 170 * x ) mod 30323)*1.0;
  40.  
  41.      RandomNumber:=trunc((frac(x/30269.0+y/30307.0+z/30323.0))*i)+1;
  42. end;
  43.  
  44. Procedure Initialize;
  45. Begin
  46.      Randomize;
  47.      x := (Random(maxint-1)+1)*1.0;           { Initialize seed values of }
  48.      y := (Random(maxint-1)+1)*1.0;           { x, y, and z.  }
  49.      z := (Random(maxint-1)+1)*1.0;
  50. end;
  51. END.
  52.